package com.lanchetech.service.impl;


import com.lanchetech.common.enums.IdentityTypeEnum;
import com.lanchetech.common.enums.UserLevelEnum;
import com.lanchetech.dao.IdentityMapper;
import com.lanchetech.dao.UserGroupMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.dao.UserRelationshipMapper;
import com.lanchetech.entity.Identity;
import com.lanchetech.entity.User;
import com.lanchetech.entity.UserGroup;
import com.lanchetech.entity.UserRelationship;
import com.lanchetech.service.CommissionService;
import com.lanchetech.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.lanchetech.common.constants.JypConstant.HIPO_USER_ID;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    UserGroupMapper userGroupMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IdentityMapper identityMapper;

    @Autowired
    UserRelationshipMapper userRelationshipMapper;

    @Autowired
    CommissionService commissionService;


    @Override
    public void createUserGroup(Long superiorId, User subordinate) {
        if (superiorId != null) {
            Identity subordinateIdentity = identityMapper.findOneByUserId(subordinate.getId());
            UserGroup result = userGroupMapper.findOneByMemberId(subordinateIdentity.getUserId());
            // 因为不要重复加入团队
            if (result == null) {
                Identity superiorIdentity = identityMapper.findOneByUserId(superiorId);
                if (superiorIdentity != null) {


                    Long shareholderId = HIPO_USER_ID;
                    if (superiorIdentity.getShareholderLevel() >= UserLevelEnum.SHAREHOLDER_L1.getLevel()) {
                        // 如上级已经是运营中心，则上级是团队的运营中心；
                        shareholderId = superiorIdentity.getUserId();
                    } else {
                        // 如果上级不是运营中心，则上级所在的团队的运营中心，则作为当前团队的运营中心；
                        shareholderId = commissionService.getSuperiorShareholder(superiorIdentity.getUserId());
                    }

                    UserGroup group = UserGroup.builder()
                            .captainId(superiorIdentity.getUserId())
                            .captainType(superiorIdentity.getType())
                            .captainLevel(superiorIdentity.getLevel())
                            .memberId(subordinateIdentity.getUserId())
                            .memberLevel(subordinateIdentity.getLevel())
                            .memberType(subordinateIdentity.getType())
                            .nickname(subordinate.getNickname())
                            .createdAt(new Date())
                            .updatedAt(new Date())
                            .orderCount(0L)
                            .amount(BigDecimal.ZERO)
                            .commission(BigDecimal.ZERO)
                            .shareholderId(shareholderId)
                            .build();
                    userGroupMapper.insert(group);
                }
            }
        }
    }

    @Override
    public void updateUserLevel(Integer level, Long userId) {
        Byte type = IdentityTypeEnum.getTypeByLevel(level);

        userMapper.updateLevelById(level, userId);

        // 更新自己作为下级的所有等级
        userGroupMapper.updateMemberLevelByMemberIdAndMemberType(level, userId, type);

        // 更新自己作为上级的所有等级
        userGroupMapper.updateCaptainLevelByCaptainIdAndCaptainType(level, userId, type);

        // 同步更新上下级表中的下级用户等级
        userRelationshipMapper.updateSubordinateLevelBySubordinateId(level, userId);

        // 如果是此人升级为运营中心了，则他团队的所有成员的上级运营中心，将都改为他
        if(UserLevelEnum.SHAREHOLDER_L1.getLevel().equals(level)){
            userGroupMapper.updateShareholderIdByCaptainId(userId, userId);
        }
    }


}
